<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
var test_cases = [
    {
        name: 'cancel',
        cancel: true,
    }, {
        name: 'accept',
        cancel: false,
        platform: 'foo',
        outcome: 'accepted'
    }, {
        name: 'dismiss',
        cancel: false,
        platform: '',
        outcome: 'dismissed'
    }
];

function banner_test(index) {
    if (index >= test_cases.length)
        return;

    var test_case = test_cases[index];
    async_test(function(t) {
        var event_handler = t.step_func(function(e) {
            // Remove the event handler to prevent it being used in subsequent
            // invocations of banner_test();
            window.removeEventListener('beforeinstallprompt', event_handler);

            assert_equals(e.platforms.length, 2, 'Number of platforms');
            assert_equals(e.platforms[0], 'foo', 'First platform');
            assert_equals(e.platforms[1], 'bar', 'Second platform');
            if (test_case.cancel) {
                e.preventDefault();
                return;
            }

            e.userChoice.then(t.step_func(function(result) {
                assert_equals(result.platform, test_case.platform, 'Resolved platform');
                assert_equals(result.outcome, test_case.outcome, 'Outcome');
                banner_test(index + 1);
                t.done();
            }));
        });
        window.addEventListener('beforeinstallprompt', event_handler);

        testRunner.dispatchBeforeInstallPromptEvent(['foo', 'bar'], t.step_func(function(result) {
            assert_equals(result, test_case.cancel, 'Event canceled');
            if (test_case.cancel) {
                banner_test(index + 1);
                t.done();
                return;
            }
            testRunner.resolveBeforeInstallPromptPromise(test_case.platform);
        }));
    }, test_case.name);
}

banner_test(0);
</script>
